package com.haochang.chunkvod.app.config;

import java.util.Locale;

import com.haochang.base.BaseApplication;
import com.haochang.chunkvod.app.common.VodConstant;
import com.haochang.chunkvod.app.info.InitType;
import com.haochang.chunkvod.app.utils.NetworkUtils;
import com.haochang.chunkvod.tools.vod.log.Logger;

import android.content.Context;
import android.content.res.Resources;
import android.os.Build.VERSION;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

/**
 * 
 * @author lxl
 *		2014-12-23-下午12:04:56
 *		设备信息配置:
 *										设备信息(像素,密度,...)
 *
 * @update xjunjie@gmail.com  2015-1-8 下午12:17:46
 * 		实现. init 细节.
 * 		更改全局变量访问只读 get* 或 Is* 接口.避免大家都在用.大家都在改的情况
 * 		添加广播监听 网络状态变化 和 Wifi 状态变化.目的：保证外部调用 DeviceConfig 时.相关信息都是系统最新信息.而非应用启动时,初始化时的信息。
 */
public class DeviceConfig
{
	private static int mDisplayWidthPixels = 0, mDisplayHeightPixels = 0;

	private static String mId, mModel, mVersion;

	private static int sysVersion = 0;

	private static InitType mInitType = InitType.None;

	private static final int verSion = 14;//手机最低系统版本号
	private static final int widthPixels = 540, heightPixels = 960;//手机最低分辨率

	private static float screen_density = 1.5f;// 屏幕密度

	private static int sizeOfSd = 50;//SD剩余剩余数

	private static InitType chickEnvironment = InitType.None;

	//设备mcc mnc
	private static String deviceMCC = "";
	private static String deviceMNC = "";

	/**
	 * 初始化
	 * @param 
	 */
	public static InitType init()
	{
		mInitType = InitType.Normal;

		if (BaseApplication.appContext != null)
		{
			try
			{
				Resources res = BaseApplication.appContext.getResources();
				mDisplayWidthPixels = res.getDisplayMetrics().widthPixels;
				mDisplayHeightPixels = res.getDisplayMetrics().heightPixels;
				screen_density = res.getDisplayMetrics().density;
			} catch (Exception ex)
			{
				ex.printStackTrace();
				mDisplayWidthPixels = 0;
				mDisplayHeightPixels = 0;
				mInitType = InitType.DeviceConfigError;
			}

			mId = getAndroidId();
			mModel = android.os.Build.MODEL;
			mVersion = VERSION.RELEASE;
			//			String mNa2 = android.os.Build.MANUFACTURER;
			NetworkUtils.initRealTime();
			NetworkUtils.setNetWorkState(new NetworkUtils().getNetworkType());

			//mcc mnc
			TelephonyManager tel = (TelephonyManager) BaseApplication.appContext.getSystemService(Context.TELEPHONY_SERVICE);
			String networkOperator = tel.getNetworkOperator();
			if (!TextUtils.isEmpty(networkOperator) && networkOperator.length() > 3)
			{
				try
				{
					deviceMCC = networkOperator.substring(0, 3);
					deviceMNC = networkOperator.substring(3);
				} catch (StringIndexOutOfBoundsException e)
				{
					deviceMCC = "";
					deviceMNC = "";
				}
			}
		} else
		{
			mInitType = InitType.DeviceConfigError;
		}

		return mInitType;
	}

	/**
	 * 开机检测情况
	 */

	public static InitType getChickEnvironment()
	{

		return chickEnvironment;
	}

	/**
	 * 获取初始化结果
	 * @return
	 */
	public static InitType getInitType()
	{
		return mInitType;
	}

	/**
	 *  获取屏幕 宽度
	 *  以 display**** 命名的目的.方便在 Eclipse 中代码联想调用
	 * @return
	 */
	public static int displayWidthPixels()
	{
		return mDisplayWidthPixels;
	}

	/**
	 * 获取屏幕 高度
	 * @return
	 */
	public static int displayHeightPixels()
	{
		return mDisplayHeightPixels;
	}

	/**
	 * 获取屏幕密度
	 * @return
	 */
	public static float getScreen_density()
	{
		return screen_density;
	}

	/**
	 * android 设备 唯一  Id
	 * @return
	 */
	public static String getId()
	{
		return mId == null ? "" : mId;
	}

	/**
	 * 设备分辨率
	 * @return
	 */
	public static String getResolution()
	{
		return String.format(Locale.getDefault(), "%d*%d", displayWidthPixels(), displayHeightPixels());
	}

	/**
	 * 设备型号
	 * @return
	 */
	public static String getModel()
	{
		return mModel == null ? "" : mModel;
	}

	/**
	 * 手机系统API版本号
	 * @return
	 */
	public static int getsysVersion()
	{
		return sysVersion;
	}

	/**
	 * 设备 android 版本
	 * @return
	 */
	public static String getVersion()
	{
		return mVersion == null ? "" : mVersion;
	}

	private static String getAndroidId()
	{
		String androidId = Secure.getString(BaseApplication.appContext.getContentResolver(), Secure.ANDROID_ID);
		if (androidId == null)
		{
			// this happens on 1.6 and older
			androidId = android.provider.Settings.System.getString(BaseApplication.appContext.getContentResolver(),
					android.provider.Settings.System.ANDROID_ID);
		}
		//Build.MODEL
		return androidId;
	}

	/**
	 * @return the deviceMCC
	 */
	public static String getDeviceMCC()
	{
		return deviceMCC;
	}

	/**
	 * @return the deviceMNC
	 */
	public static String getDeviceMNC()
	{
		return deviceMNC;
	}

	/**
	 * 判断是否使用P2000的设备
	 * @return
	 */
	public static boolean isUseP2000Device()
	{
		String model = android.os.Build.MODEL;
		String manufacturer = android.os.Build.MANUFACTURER;
		String device = android.os.Build.DEVICE;
		Logger.e("model:" + model + "   manufacturer:" + manufacturer + "   device:" + device);
		if (TextUtils.equals(model, VodConstant.MODEL) && TextUtils.equals(manufacturer, VodConstant.MANUFACTURER)
				&& TextUtils.equals(device, VodConstant.DEVICE))
			return true;
		else
			return false;
	}

}
